package self.Lunastic.HexaSweeper.Manager;

import self.Lunastic.HexaSweeper.Window.WindowControl;
import self.Lunastic.HexaSweeper.Window.WindowGame;

import javax.swing.*;
import java.awt.event.*;


/**
 * 所有对屏幕的管理 与流程控制
 */
public class FrameManager{
    private static int _width = 1500;
    private static int _height = 900;
    public static void setWidth(int width) { FrameManager._width = width; }
    public static void setHeight(int height) { FrameManager._height = height; }
    public static int getWidth() { return _width; }
    public static int getHeight() { return _height; }

    // 框架对象
    public static JFrame jFrame;
    // 窗口对象
    private final static WindowGame windowGame = new WindowGame(-150, 0, getWidth(), getHeight());
    private final static WindowControl windowControl = new WindowControl(getWidth() - 360, 0, 360, getHeight());// 框架运行
    public static void run(){
        jFrame = new JFrame();
        initFrame();
        GameManager.reset();
        createWindowGame();
        createWindowControl();
    }

    private static void initFrame(){
        jFrame.setTitle("HexaSweeper_" + GameManager.VERSION);
        jFrame.setSize(FrameManager.getWidth(), FrameManager.getHeight());
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(false);
        jFrame.setLayout(null);
        jFrame.getContentPane().setBackground(StyleOption.BACKGROUND);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 焦点事件
        jFrame.addWindowFocusListener(new WindowFocusListener() {
            // 获得焦点
            @Override
            public void windowGainedFocus(WindowEvent e) { GameManager.continued(); }

            // 失去焦点
            @Override
            public void windowLostFocus(WindowEvent e) { GameManager.paused(); }
        });
    }

    // 创建各种窗口
    private static void createWindowControl(){ jFrame.getContentPane().add(windowControl); }
    private static void createWindowGame(){ jFrame.getContentPane().add(windowGame); }

    public static void resetWindowGame(){
        windowGame.initArea();
        windowGame.active();
    }
    public static void stopWindowGame(){ FrameManager.windowGame.disactive(); }

    public static void framePaused(){ windowGame.disactive(); }

    public static void frameContinued(){ windowGame.active(); }
}

